package cn.wolfcode.web.controller;

import cn.wolfcode.domain.Classinfo;
import cn.wolfcode.domain.Department;
import cn.wolfcode.domain.Employee;
import cn.wolfcode.quest.ClassinfoQueryObject;
import cn.wolfcode.quest.JsonResult;
import cn.wolfcode.service.IClassinfoService;
import cn.wolfcode.service.IDepartmentService;
import cn.wolfcode.service.IEmployeeService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashSet;
import java.util.List;

@Controller
@RequestMapping("/classinfo")
public class ClassInfoController {

   @Autowired
    private IClassinfoService classinfoService;
   @Autowired
   private IDepartmentService departmentService;
   @Autowired
   private IEmployeeService employeeService;

   @RequestMapping("/list")
    public String ListAll(Model model, @ModelAttribute("qo") ClassinfoQueryObject qo){
            if(qo.getEmployeeId()==null){
                qo.setEmployeeId(-1L);
            }
             PageInfo<Classinfo> query = classinfoService.query(qo);
             model.addAttribute("pageResult",query);

       List<Classinfo> classinfos = classinfoService.selectAll();
           Long []ids=new Long[classinfos.size()];
       HashSet<Long> set = new HashSet<>();
       for (int i=0;i<classinfos.size();i++){
                 set.add(classinfos.get(i).getEmployee().getId());

          }
    /*   List<Employee> employees = employeeService.selectAll();*/
       List<Employee> lists=employeeService.selectByIds(set);
          model.addAttribute("employee",lists);


       return "classinfo/list";

   }
     /*
              通过id删除元素
      */
   @RequestMapping("/delete")
    @ResponseBody
    public JsonResult delById(Long id){
       classinfoService.deleteById(id);
       return new JsonResult(true,"删除元素成功");
   }

    /*
          保存或者添加操作
     */
    @RequestMapping("/saveOrUpdate")
    @ResponseBody
    public JsonResult saveOrUpdate(Classinfo classinfo){
         if(classinfo.getId()!=null){
               classinfoService.update(classinfo);
         }else {
                classinfoService.insert(classinfo);
         }
         return new JsonResult(true,"编辑或者添加成功");
    }


}
